package at.fhooe.mc.msports;

import net.rim.device.api.database.Cursor;
import net.rim.device.api.database.Database;
import net.rim.device.api.database.Row;
import net.rim.device.api.database.Statement;

public class PersonalData {

	public static int MIN_WEIGHT = 40;
	public static int MAX_WEIGHT = 180;
	public static int MIN_HEIGHT = 130;
	public static int MAX_HEIGHT = 230;
	public static int MIN_AGE = 10;
	public static int MAX_AGE = 110;

	private static float hrMax;
	private static float height;
	private static float weight;
	private static int age;
	private static boolean useGPS;
	private static boolean male;
	private static int zone;
	

	private static Database database;

	public static void setDatabase(Database db) {
		database = db;
	}

	public static boolean updateData() {
		Statement stmt;
		int gps=useGPS()?1:0;
		int male=isMale()?1:0;
		try {
			if (isEmpty()) {
				stmt = database
						.createStatement("INSERT INTO PersonalData VALUES("
								+ getHeight() + "," + getWeight() + "," + getAge() + ","
								+ gps + ","+male+","+ getZone() +")");
			} else {
				stmt = database
						.createStatement("UPDATE PersonalData set Height="
								+ getHeight() + ",Weight=" + getWeight() + ",Age=" + getAge()
								+ ",GPS=" + gps+",MALE="+male + ",Zone=" + getZone());
			}

			stmt.prepare();
			stmt.execute();
			stmt.close();

			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	public static void loadData() {
		if (isEmpty())
			return;
		try {
			Statement stmt = database
					.createStatement("SELECT * FROM PersonalData");
			stmt.prepare();
			Cursor c = stmt.getCursor();
			Row r;
			while(c.next()){
				r = c.getRow();
				setHeight(r.getFloat(0));
				setWeight(r.getFloat(1));
				setAge(r.getInteger(2));
				setGPS(r.getBoolean(3));
				setMale(r.getBoolean(4));
				setZone(r.getInteger(5));
			}
		} catch (Exception e) {
			e.printStackTrace();
			
		}
	}

	public static boolean isEmpty() {
		try {
			Statement stmt = database
					.createStatement("SELECT Height FROM PersonalData");
			stmt.prepare();
			Cursor c = stmt.getCursor();
			return !c.next();
		} catch (Exception e) {
			System.out.print(e.getMessage());
			return true;
		}
	}
	public static boolean isMale() {
		return male;
	}

	public static void setMale(boolean male) {
		PersonalData.male = male;
	}
	public static void setHeight(float h) {
		height = h;
	}

	public static void setWeight(float w) {
		weight = w;
	}

	public static void setAge(int a) {
		age = a;
		hrMax = 208 - 0.7f*age;
	}

	public static float getHeight() {
		return height;
	}

	public static float getWeight() {
		return weight;
	}

	public static int getAge() {
		return age;
	}

	public static void setGPS(boolean gps) {
		useGPS = gps;
	}
	
	public static float getHRMax(){
		return hrMax;
	}
	
	public static boolean useGPS() {
		return useGPS;
	}
	public static float getV02Max(){
		
		int genderFactor = 0;
		
		if(isMale())
			genderFactor = 1;
		
		float predV02Max = (float) ((0.133f * getAge()) - (0.005*getAge()*getAge()) +
			(11.403f * genderFactor) + (1.463f * getZone()) + (9.17f * getHeight()/100.0f)
			- (0.254*getWeight()) + 34.143f);
		
		return predV02Max;
		
	}
	
	public static void setZone(int z){
		zone = z;
	}
	
	public static int getZone(){
		return zone;
	}
	
}
